package com.idea.common.swing;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

import com.idea.resources.general.GeneralIcons;

public class FileToolUI implements KeyListener { // implements ActionListener 

	private FileTools editor;
	private JMenu fileMenu;
	private JToolBar toolBar;

	private Action newAction;
	private Action openAction;
	private Action saveAction;
	private Action saveAsAction;
	private Action exitAction;

	private Action closeAction;
	private Action cloneAction;

	public FileToolUI(FileTools editor) {
		this.editor = editor;
		createActions();

		toolBar = new JToolBar();
		fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);

		JButton newButton = toolBar.add(newAction);
		JMenuItem newMenuItem = fileMenu.add(newAction);
		newMenuItem.setMnemonic(KeyEvent.VK_N);
		newButton.setToolTipText("Create new file");

		JButton openButton = toolBar.add(openAction);
		JMenuItem openMenuItem = fileMenu.add(openAction);
		openButton.setToolTipText("Open file");
		openMenuItem.setMnemonic(KeyEvent.VK_O);

		JButton saveButton = toolBar.add(saveAction);
		JMenuItem saveMenuItem = fileMenu.add(saveAction);
		saveButton.setToolTipText("Save file");
		saveMenuItem.setMnemonic(KeyEvent.VK_S);

		JButton saveAsButton = toolBar.add(saveAsAction);
		JMenuItem saveAsMenuItem = fileMenu.add(saveAsAction);
		saveAsButton.setToolTipText("Save file as...");

		JButton cloneButton = toolBar.add(cloneAction);
		JMenuItem cloneMenuItem = fileMenu.add(cloneAction);
		saveAsButton.setToolTipText("Clone");

		JButton closeButton = toolBar.add(closeAction);
		JMenuItem closeMenuItem = fileMenu.add(closeAction);
		saveAsButton.setToolTipText("Close file");

		//	JButton exitButton = toolBar.add(exitAction);
		JMenuItem exitMenuItem = fileMenu.add(exitAction);
		//saveAsButton.addActionListener(this);
		//add(exitButton);
	}

	public void createActions() {
		newAction = new AbstractAction("New", GeneralIcons.newIcon) {
			public void actionPerformed(ActionEvent event) {
				editor.newFile();
			}
		};
		openAction = new AbstractAction("Open", GeneralIcons.openIcon) {
			public void actionPerformed(ActionEvent event) {
				editor.openFile();
			}
		};
		saveAction = new AbstractAction("Save", GeneralIcons.saveIcon) {
			public void actionPerformed(ActionEvent event) {
				editor.saveFile();
			}
		};
		saveAsAction =
			new AbstractAction("Save As...", GeneralIcons.saveAsIcon) {
			public void actionPerformed(ActionEvent event) {
				editor.saveAsFile();
			}
		};
		exitAction = new AbstractAction("Exit") {
			public void actionPerformed(ActionEvent event) {
				editor.exit();
			}
		};

		cloneAction = new AbstractAction("Clone", GeneralIcons.cloneIcon) {
			public void actionPerformed(ActionEvent event) {
				editor.cloneFile();
			}
		};

		closeAction = new AbstractAction("Close", GeneralIcons.closeIcon) {
			public void actionPerformed(ActionEvent event) {
				editor.closeFile();
			}
		};
	}
	public JMenu getFileMenu() {
		return fileMenu;
	}

	public JToolBar getToolBar() {
		return toolBar;
	}

	public Action getExitAction() {
		return exitAction;
	}

	public void setExitAction(Action action) {
		exitAction = action;
	}

	public Action getNewAction() {
		return newAction;
	}

	public Action getOpenAction() {
		return openAction;
	}

	public Action getSaveAction() {
		return saveAction;
	}

	public Action getSaveAsAction() {
		return saveAsAction;
	}

	public void keyTyped(KeyEvent event) {
	}

	public void keyPressed(KeyEvent event) {

		int modifiers = event.getModifiers();

		String mod = KeyEvent.getKeyModifiersText(modifiers);
		if (!mod.equals("Ctrl")) {
			return;
		}
		switch (event.getKeyCode()) {
			case KeyEvent.VK_N :
				editor.newFile();
				break;
			case KeyEvent.VK_S :
				editor.saveFile();
				break;
			default :
				}
	}

	public void keyReleased(KeyEvent event) {
	}

}